package com.talking.activities
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.github.dhaval2404.imagepicker.ImagePicker
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.talking.App
import com.talking.R
import com.talking.bean.PersonBean
import com.talking.bean.UserBean
import com.talking.databinding.ActivityPersonBinding
import com.talking.util.BlurTransformation
import com.talking.util.Network
import com.talking.util.Tools
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.File
import java.io.IOException


class PersonActivity : AppCompatActivity() {
    private var user_qr: UserBean=UserBean()
    private lateinit var launcher: ActivityResultLauncher<Intent>
    private lateinit var binding: ActivityPersonBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityPersonBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.personQrcode.setOnClickListener {launcher.launch(Intent(this,QrcodeActivity::class.java).putExtra("user",user_qr), ActivityOptionsCompat.makeSceneTransitionAnimation(this, binding.personQrcode, "qrcode")) }
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->

        }
        refresh()
    }

    private fun refresh() {
        val other_mail = intent.getStringExtra("mail")!!
        if (App.me.mail.toString().equals(other_mail)){
            setPicture(App.me.iconUrl)
            binding.personOperation.visibility=View.GONE
            binding.personNickname.text=App.me.nickname
            binding.personAccount.text=App.me.mail

            user_qr=App.me



        }else{
            val map = HashMap<String, String>()
            map["me_mail"] = App.me.mail.toString()
            map["other_mail"] = other_mail
            Network.post(map, "user/getPerson/") { call: Call ->
                call.enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        e.printStackTrace()
                        runOnUiThread {
                            Tools.toast(e.message)
                        }
                    }
                    override fun onResponse(call: Call, response: Response) {
                        runOnUiThread{
                            val jo = JsonParser.parseString(response.body!!.string()).asJsonObject
                            if (jo.get("msg").asString.equals("获取成功")){
                                val person = Gson().fromJson(jo.get("data").asJsonObject, PersonBean::class.java)

                                user_qr.iconUrl=person.iconUrl
                                user_qr.mail=person.mail
                                user_qr.nickname=person.nickname


                                setPicture(person.iconUrl)
                                binding.personOperation.visibility=View.VISIBLE
                                if (person.isFriend){
                                    binding.personButtonLeft.text = getString(R.string.chat)
                                    binding.personButtonRight.text = getString(R.string.remark)
                                    binding.personDelete.visibility=View.VISIBLE
                                }else{//stranger
                                    binding.personButtonLeft.visibility= View.GONE
                                    binding.personButtonRight.text = getString(R.string.add_friend)
                                }
                                binding.personNickname.text=person.nickname
                                binding.personAccount.text=person.mail

                            }else{
                                Tools.toast(jo.get("msg").asString)
                            }
                        }
                    }
                })
            }
        }

    }

    private fun setPicture(iconUrl: String?) {
        Glide.with(this).load(Network.BASE_URL+ iconUrl).apply(Tools.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.personIcon)
        Glide.with(this).load(Network.BASE_URL+ iconUrl).apply(RequestOptions.bitmapTransform(BlurTransformation(this@PersonActivity, 25, 8))).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.personIconBg)
    }
}